package com.share;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.overzealous.remark.Remark;
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc:
 * @author:caifan
 * @date:2022/4/21
 */
public class GeekbangCourse {
    public static void main(String[] args) throws Exception {
        Connection connection = Jsoup.connect("https://time.geekbang.org/column/article/95868");
        Map<String, String> map = new HashMap<>();
        map.put("Accept", "application/json, text/plain, */*");
        map.put("Accept-Encoding", "gzip, deflate, br");
        map.put("Accept-Language", "zh-CN,zh;q=0.9");
        map.put("Connection", "keep-alive");
        map.put("Content-Type", "application/json");
        map.put("Cookie", "MEIQIA_TRACK_ID=1T19Z1hWSGE8vKBEjaVW1oycqYR; MEIQIA_VISIT_ID=1VEfJfd92yN7dqu5dI0HlaxLrGR; GCID=8d88e16-6b1b670-696d72f-1e3b787; GRID=8d88e16-6b1b670-696d72f-1e3b787; gksskpitn=33e8a50e-ca7e-4b72-9669-02d5aab75c60; LF_ID=1650553014671-6816037-6736365; Hm_lvt_59c4ff31a9ee6263811b23eb921a5083=1650553015; Hm_lvt_022f847c4e3acd44d4a2481d9187f1e6=1650553015; _gid=GA1.2.1599392864.1650553015; _ga=GA1.2.1374715084.1650553015; GCESS=BgkBAQQEAC8NAAUEAAAAAAcEbr3oSgsCBgAKBAAAAAAGBL_MmWQCBNJwYWINAQEDBNJwYWIIAQMBCPtmEAAAAAAADAEB; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221074939%22%2C%22first_id%22%3A%221804ca048ebb7e-0e7d5d739ed41f-521e311e-1327104-1804ca048ec9ac%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Faccount.geekbang.com%2F%22%2C%22%24latest_landing_page%22%3A%22https%3A%2F%2Ftime.geekbang.org%2F%22%7D%2C%22%24device_id%22%3A%221804ca048ebb7e-0e7d5d739ed41f-521e311e-1327104-1804ca048ec9ac%22%7D; gk_exp_uid=YWMxOTdhZTk5MDIxNDA0ZjZmNGVlMDI2MzZlNzE1NTY=|1650640478826932164|556d146031a295485244c481cb77ff4755dfad30383abd5f554ea5a92535d713; Hm_lpvt_022f847c4e3acd44d4a2481d9187f1e6=1650679055; _gat=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221074939%22%2C%22first_id%22%3A%221804ca048ebb7e-0e7d5d739ed41f-521e311e-1327104-1804ca048ec9ac%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_landing_page%22%3A%22https%3A%2F%2Ftime.geekbang.org%2Fcolumn%2Farticle%2F95868%22%7D%2C%22%24device_id%22%3A%221804ca048ebb7e-0e7d5d739ed41f-521e311e-1327104-1804ca048ec9ac%22%7D; gk_process_ev={%22referrer%22:%22%22%2C%22utime%22:1650636825193%2C%22count%22:24%2C%22target%22:%22%22}; SERVERID=3431a294a18c59fc8f5805662e2bd51e|1650679348|1650679049; Hm_lpvt_59c4ff31a9ee6263811b23eb921a5083=1650679339");
        map.put("sec-ch-ua-platform", "Windows");
        map.put("Sec-Fetch-Dest", "empty");
        map.put("Sec-Fetch-Mode", "cors");
        map.put("Sec-Fetch-Site", "same-origin");
        map.put("User-Agent", "Mozilla/5.0");
        connection.headers(map);
        System.out.println(connection.execute().body());
//        System.out.println(document.body().toString());
    }

    @Test
    public void postTest() throws Exception {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        HttpPost httpPost = new HttpPost("https://time.geekbang.org/serv/v1/article");

        httpPost.addHeader("Accept", "application/json, text/plain, */*");
        httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
        httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
        httpPost.addHeader("Connection", "keep-alive");
//        httpPost.addHeader("Content-Length", "60");
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Cookie", "MEIQIA_TRACK_ID=1T19Z1hWSGE8vKBEjaVW1oycqYR; MEIQIA_VISIT_ID=1VEfJfd92yN7dqu5dI0HlaxLrGR; GCID=8d88e16-6b1b670-696d72f-1e3b787; GRID=8d88e16-6b1b670-696d72f-1e3b787; gksskpitn=33e8a50e-ca7e-4b72-9669-02d5aab75c60; LF_ID=1650553014671-6816037-6736365; Hm_lvt_59c4ff31a9ee6263811b23eb921a5083=1650553015; Hm_lvt_022f847c4e3acd44d4a2481d9187f1e6=1650553015; _gid=GA1.2.1599392864.1650553015; _ga=GA1.2.1374715084.1650553015; GCESS=BgkBAQQEAC8NAAUEAAAAAAcEbr3oSgsCBgAKBAAAAAAGBL_MmWQCBNJwYWINAQEDBNJwYWIIAQMBCPtmEAAAAAAADAEB; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221074939%22%2C%22first_id%22%3A%221804ca048ebb7e-0e7d5d739ed41f-521e311e-1327104-1804ca048ec9ac%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Faccount.geekbang.com%2F%22%2C%22%24latest_landing_page%22%3A%22https%3A%2F%2Ftime.geekbang.org%2F%22%7D%2C%22%24device_id%22%3A%221804ca048ebb7e-0e7d5d739ed41f-521e311e-1327104-1804ca048ec9ac%22%7D; gk_exp_uid=YWMxOTdhZTk5MDIxNDA0ZjZmNGVlMDI2MzZlNzE1NTY=|1650640478826932164|556d146031a295485244c481cb77ff4755dfad30383abd5f554ea5a92535d713; Hm_lpvt_022f847c4e3acd44d4a2481d9187f1e6=1650679055; _gat=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221074939%22%2C%22first_id%22%3A%221804ca048ebb7e-0e7d5d739ed41f-521e311e-1327104-1804ca048ec9ac%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_landing_page%22%3A%22https%3A%2F%2Ftime.geekbang.org%2Fcolumn%2Farticle%2F95868%22%7D%2C%22%24device_id%22%3A%221804ca048ebb7e-0e7d5d739ed41f-521e311e-1327104-1804ca048ec9ac%22%7D; gk_process_ev={%22referrer%22:%22%22%2C%22utime%22:1650636825193%2C%22count%22:24%2C%22target%22:%22%22}; SERVERID=3431a294a18c59fc8f5805662e2bd51e|1650679348|1650679049; Hm_lpvt_59c4ff31a9ee6263811b23eb921a5083=1650679339");
        httpPost.addHeader("sec-ch-ua-platform", "Windows");
        httpPost.addHeader("Sec-Fetch-Dest", "empty");
        httpPost.addHeader("Sec-Fetch-Mode", "cors");
        httpPost.addHeader("Sec-Fetch-Site", "same-origin");
        httpPost.addHeader("User-Agent", "Mozilla/5.0");
//        httpPost.addHeader("", "");

        JSONObject json = new JSONObject();
        json.put("id", "95868");
        json.put("include_neighbors", true);
        json.put("is_freelyread", true);
        HttpEntity httpEntity = new StringEntity(json.toJSONString());
        httpPost.setEntity(httpEntity);

        CloseableHttpResponse response = httpClient.execute(httpPost);
        System.out.println(response.getStatusLine().getStatusCode());
//        response.getEntity()
    }

    @Test
    public void createFileTest() throws IOException {
        String dir = "D:/geek";
        File file = new File(dir + File.separator + "eBPF核心技术与实战-倪朋飞");
        if (!file.exists()) {
            file.mkdirs();
        }
        System.out.println(file.getPath());
        for (int i = 1;i<=18; i++) {
            File newFile = new File(file.getPath() + File.separator + i + ".txt");
            newFile.createNewFile();
        }
    }

    @Test
    public void convertTest() throws Exception {
        String rootDir = "D:/geek";
        String allStr = Files.readString(Path.of(rootDir + "/程序员的数学基础课-黄申/54.txt"));
        if (StringUtils.isEmpty(allStr)) {
            System.out.println("file:" + "filepath");
            return;
        }
        JSONObject jsonObject = JSON.parseObject(allStr);
        JSONObject data = jsonObject.getJSONObject("data");
        String title = data.getString("article_title");
        String content = data.getString("article_content");
        content = content.replace("<img src=\"", "![](").replace("\" alt=\"\">", ")");
        System.out.println("title:" + title);
        System.out.println("content:" + content);
        title = title.replace(" ", "").replace("|", "-").replace("｜", "-")
                .replace("\\|", "-").replace(":", "_").replace("\\", "-")
                .replace("*", "").replace("<", "(").replace(">", ")").replace("\b", "")
                .replace("\\", "-").replace("/", "-").replace("?", "");
        PrintWriter pw = new PrintWriter(rootDir + File.separator + "geek_new/" + title + ".md");
        pw.write(content);
        pw.flush();
        pw.close();
    }

    @Test
    public void coverTest() {
        convert(Path.of("D:/geek"), "D:/geek_md");
    }

    private void convert(Path filePath, String newPath) {
        try {
            Files.list(filePath).forEach(path -> {
                try {
                    if (Files.notExists(Path.of(newPath + File.separator + filePath.getFileName()))) {
                        Files.createDirectories(Path.of(newPath + File.separator + filePath.getFileName().toString()));
                    }
                    if (Files.isDirectory(path)) {
                        convert(path, newPath);
                    }

                    if (path.toFile().isFile()) {
                        String allStr = Files.readString(path);
                        if (StringUtils.isEmpty(allStr)) {
                            return;
                        }
                        JSONObject jsonObject = JSON.parseObject(allStr);
                        JSONObject data = jsonObject.getJSONObject("data");
                        String title = data.getString("article_title");
                        String content = data.getString("article_content");
                        content = content.replace("<img src=\"", "![](").replace("\" alt=\"\">", ")");
                        title = title.replace(" ", "").replace("｜", "-").replace("｜", "-")
                                .replace("|", "-").replace("\\|", "-").replace(":", "_")
                                .replace("?", "").replace("\\", "_").replace("/", "-");
                        String name = path.getFileName().toFile().getName();
                        name = name.substring(0, name.indexOf("."));
                        PrintWriter pw = new PrintWriter(newPath + File.separator + filePath.getFileName() + File.separator + String.format("%02d", Integer.parseInt(name)) + "-" + title + ".md");
                        pw.write(content);
                        pw.flush();
                        pw.close();
                    }
                } catch (Exception e) {
                    System.out.println(path);
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            System.out.println("path:" + filePath);
            e.printStackTrace();
        }
    }

    @Test
    public void pathTest() {
        Path path = Path.of("D:\\geek\\A-B测试从0到1-张博伟\\1.txt");
        System.out.println(path.getFileName());
    }

    @Test
    public void fileTest01() {
        String str = "09丨|b关联和断言：一动一静，核心都是在取数据.md";
        System.out.println(str.replace("｜", "-").replace("|", "-").replace("\\", ""));
    }

    @Test
    public void convertHtml2Md() throws Exception {
        String path = "D:/geek_md/java并发编程实战-王宝令/09-07-安全性、活跃性以及性能问题.md";
        String noUse = "<!-- [[[read_end]]] -->";
        String content = IOUtils.toString(new FileInputStream(path), Charset.defaultCharset());
        content = content.replace(noUse, "");
        Remark remark = new Remark();
        content = remark.convert(content);
        PrintWriter pw = new PrintWriter(path);
        pw.write(content);
        pw.flush();
        pw.close();

    }

    @Test
    public void codeTest() {
        String href = "<p data-nodeid=\"8807\">在“<a href=\"https://kaiwu.lagou.com/course/courseInfo.htm?courseId=595#/detail/pc?id=6133\" data-nodeid=\"8812\">第 8 讲</a>”里，我们介绍了<strong data-nodeid=\"8818\">代理式的分库分表的架构方案：分库代理中间件解析用户指定的 SQL 并提取路由字段，根据路由字段去访问具体的分库进行数据的查询</strong>。如下图 2 所示：</p> <p data-nodeid=\"10619\" class=\"\"><img src=\"https://s0.lgstatic.com/i/image6/M00/04/34/CioPOWAkHl2AfaSRAADhJcnazpA719.png\" alt=\"图片2.png\" data-nodeid=\"10623\"></p> <div data-nodeid=\"10620\"><p style=\"text-align:center\">图 2：代理式分库分表方案</p></div>";
        Remark remark = new Remark();
        System.out.println(remark.convert(href));
    }

    @Test
    public void html2Md() {
        convertMarkdown(Path.of("D:\\lagou"));
    }

    private void convertMarkdown(Path filePath) {
        try {
            Files.list(filePath).forEach(path -> {
                try {
                    if (Files.isDirectory(path)) {
                        convertMarkdown(path);
                    }

                    if (path.toFile().isFile()) {
                        String allStr = Files.readString(path);
                        if (StringUtils.isEmpty(allStr)) {
                            return;
                        }
                        FlexmarkHtmlConverter build = FlexmarkHtmlConverter.builder().build();
                        String content = build.convert(allStr);
                        content = content.replace("\\", "");
                        PrintWriter pw = new PrintWriter(path.toString());
                        pw.write(content);
                        pw.flush();
                        pw.close();
                    }
                } catch (Exception e) {
                    System.out.println(path);
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            System.out.println("path:" + filePath);
            e.printStackTrace();
        }
    }
}
